import Vue from 'vue'
import VueRouter from 'vue-router'
//inmort OfficeSearch from '@/views/HomeView/OfficeSearch.vue'
//inmort AcademySearch from '@/views/HomeView/AcademySearch.vue'
import ProfileView from '@/views/ProfileView/ProfileView.vue'
import ProfileView2 from '@/views/ProfileView2/ProfileView.vue'
import TalkView from '@/views/TalkView/TalkView.vue'
import ThemeList from '@/views/TalkView/ThemeList.vue'
import TalkSelect from '@/views/TalkView/TalkSelect.vue'
import EditBlog from '@/views/TalkView/EditBlog.vue'
import InnerFile from '@/views/ProfileView/InnerFile.vue'
import InnerFile2 from '@/views/ProfileView2/InnerFile.vue'
import Login from '@/components/Login.vue'
import Layout from '@/components/Layout.vue'
import Register from '@/components/Register.vue'
import ResetPassword from '@/components/ResetPassword.vue'

// 后台
import AdHome from '@/views/admin/AdHome.vue'
import AdInfo from '@/views/admin/AdInfo.vue'
import ChangePassword from '@/views/admin/ChangePassword.vue'
import Usermanage from '@/views/admin/Usermanage.vue'
import Editormanage from '@/views/admin/Editormanage.vue'
import Servicemanage from '@/views/admin/Servicemanage.vue'
import AdApply from '@/views/admin/AdApply.vue'
import PerJournal from '@/views/admin/PerJournal.vue'
import HandleIdeas from '@/views/admin/HandleIdeas.vue'
import AllJournal from '@/views/admin/AllJournal.vue'
import AcStar from '@/views/admin/AcStar.vue'
import BlogNews from '@/views/admin/BlogNews.vue'
import BlogPub from '@/views/admin/BlogPub.vue'
import BlogStar from '@/views/admin/BlogStar.vue'
import BlogHistory from '@/views/admin/BlogHistory.vue'

import EdHome from '../views/editor/EdHome.vue'
import EdInfo from '../views/editor/EdInfo.vue'
import changePassword from '@/views/admin/ChangePassword.vue'

import SeHome from '../views/service/SeHome.vue'
import SeInfo from '../views/service/SeInfo.vue'
import Changepassword from '@/views/admin/ChangePassword.vue'

import UserHome from '../views/user/UserHome.vue'
import UserInfo from '../views/user/UserInfo.vue'
import changepassword from '@/views/admin/ChangePassword.vue'

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err)
}

Vue.use(VueRouter)

const routes = [
	// =========登录=========
	{
		path: '/login',
		name: 'Login',
		component: Login
	},

	{
		path: '/search',
		name: 'Search',
		component: () => import('@/components/SearchView.vue')
	},

	{
		path: '/search2',
		name: 'Search2',
		component: () => import('@/components/SearchView2.vue')
	},
	// { path: '/', redirect: '/login' },
	// =========主页=========

	{
		path: '/',
		name: 'Layout',
		component: Layout,

		redirect: '/home',
		children: [
			{
				path: 'profile',
				// name: 'home',
				component: ProfileView,
				children: [
					{
						path: 'editProfile',
						// name: 'editProfile',
						component: InnerFile
					}
				]
			},

			{
				path: 'profile2',
				// name: 'home',
				component: ProfileView2,
				children: [
					{
						path: 'editProfile',
						// name: 'editProfile',
						component: InnerFile2
					}
				]
			},

			{
				path: 'talk',
				// name: 'home',
				component: TalkSelect
			},
			{
				path: 'help',
				// name: 'home',
				component: () => import('@/views/help/All.vue'),
				redirect: '/help/aboutus',
				// component: Help,
				children: [
					{
						path: 'aboutus',
						component: () => import('@/views/help/AboutUs.vue')
					},
					{
						path: 'commonproblem',
						component: () => import('@/views/help/CommonProblem.vue')
					},
					{
						path: 'serve',
						component: () => import('@/views/help/Serve.vue')
					},
					{
						path: 'update',
						component: () => import('@/views/help/Update.vue')
					}
				]
			},
			{
				path: 'academy',
				// name: 'home',
				component: () => import('@/views/AcademyView/All.vue'),
				redirect: '/academy/vein',
				// component: AcademyView,
				children: [
					{
						path: 'vein',
						component: () => import('@/views/AcademyView/Vein.vue')
					},
					{
						path: 'map',
						component: () => import('@/views/AcademyView/Map.vue')
					},
					{
						path: 'distribute',
						component: () => import('@/views/AcademyView/Distribute.vue')
					}
				]
			},
			{
				path: 'office',
				// name: 'home',
				component: () => import('@/views/OfficeView/All.vue'),
				redirect: '/office/vein',
				// component: OfficeView,
				children: [
					{
						path: 'vein',
						component: () => import('@/views/OfficeView/Vein.vue')
					},
					{
						path: 'map',
						component: () => import('@/views/OfficeView/Map.vue')
					},
					{
						path: 'distribute',
						component: () => import('@/views/OfficeView/Distribute.vue')
					}
				]
			},

			{
				path: 'home',
				// name: 'home',
				component: () => import('@/views/HomeView/All.vue'),
				redirect: '/home/office',
				// component: OfficeView,
				children: [
					{
						path: 'office',
						component: () => import('@/views/HomeView/OfficeSearch.vue')
					},
					{
						path: 'academy',
						component: () => import('@/views/HomeView/AcademySearch.vue')
					}
				]
			},
			{
				path: 'blogs',
				// name: 'home',
				component: TalkView
			},
			{
				path: 'editBlog',
				// name: 'home',
				component: EditBlog
			}
		]
	},
	{ path: '/resetpassword', component: ResetPassword },
	{ path: '/register', component: Register },
	{
		// 管理员页面
		path: '/adhome',
		component: AdHome,
		redirect: '/adinfo',
		children: [
			{
				path: '/adinfo',
				component: AdInfo,
			},
			{
				path: 'changepassword',
				component: ChangePassword
			},
			{
				path: 'usermanage',
				component: Usermanage
			},
			{
				path: 'editormanage',
				component: Editormanage
			},
			{
				path: 'servicemanage',
				component: Servicemanage
			},
			{
				path: 'adapply',
				component: AdApply
			},
			{
				path: 'perjournal',
				component: PerJournal
			},
			{
				path: 'handleidesa',
				component: HandleIdeas
			},
			{
				path: 'alljournal',
				component: AllJournal
			},
			{
				path: 'acstar',
				component: AcStar
			},
			{
				path: 'blognews',
				component: BlogNews
			},
			{
				path: 'blogpub',
				component: BlogPub
			},
			{
				path: 'blogstar',
				component: BlogStar
			},
			{
				path: 'bloghistory',
				component: BlogHistory
			}
		]
	},
	{
		//编辑页面
		path: '/edhome',
		component: EdHome,
		redirect: '/edinfo',
		children: [
			{
				path: '/edinfo',
				component: EdInfo
			},
			{
				path: 'changepassword',
				component: changePassword
			},
			{
				path: 'acstar',
				component: AcStar
			},
			{
				path: 'blognews',
				component: BlogNews
			},
			{
				path: 'blogpub',
				component: BlogPub
			},
			{
				path: 'blogstar',
				component: BlogStar
			},
			{
				path: 'bloghistory',
				component: BlogHistory
			},
			{
				path: 'perjournal',
				component: PerJournal
			}
		]
	},
	
		{
		//客服页面
		path: '/sehome',
		component: SeHome,
		redirect: '/seinfo',
		children: [
			{
				path: '/seinfo',
				component: SeInfo
			},
			{
				path: 'changepassword',
				component: Changepassword
			},
			{
				path: 'acstar',
				component: AcStar
			},
			{
				path: 'blognews',
				component: BlogNews
			},
			{
				path: 'blogpub',
				component: BlogPub
			},
			{
				path: 'blogstar',
				component: BlogStar
			},
			{
				path: 'bloghistory',
				component: BlogHistory
			},
			{
				path: 'handleideas',
				component: HandleIdeas
			}
		]
	},
	{
		//用户页面
		path: '/userhome',
		component: UserHome,
		redirect: '/userinfo',
		children: [
			{
				path: '/userinfo',
				component: UserInfo
			},
			{
				path: 'changepassword',
				component: changepassword
			},
			{
				path: 'acstar',
				component: AcStar
			},
			{
				path: 'blognews',
				component: BlogNews
			},
			{
				path: 'blogpub',
				component: BlogPub
			},
			{
				path: 'blogstar',
				component: BlogStar
			},
			{
				path: 'bloghistory',
				component: BlogHistory
			}
		]
	}

	// {
	//   path: '',
	//   // name: 'home',
	//   component: HomeView,
	// },
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})

router.afterEach((to, from, next) => {
	document.querySelector('body').setAttribute('style', 'overflow: auto !important;')
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()放行    next('/login')强制跳转..

  // 获取token
  let token = window.localStorage.token;
  let status = window.localStorage.status;
  if (token === 'null' || token === '' || token === undefined) { // 未登录的情况
	if(to.path === '/adinfo' || to.path === '/userinfo' || to.path === '/edinfo' || to.path === '/seinfo'
	
	|| to.path === '/adinfo/changepassword' || to.path === '/adinfo/usermanage' || to.path === '/adinfo/editormanage' || to.path === '/adinfo/servicemanage'
	|| to.path === '/adinfo/adapply' || to.path === '/adinfo/perjournal' || to.path === '/adinfo/alljournal' || to.path === '/adinfo/handleideas'
	|| to.path === '/adinfo/acstar' || to.path === '/adinfo/blognews' || to.path === '/adinfo/blogpub'
	|| to.path === '/adinfo/blogstar' || to.path === '/adinfo/bloghistory'
	
	|| to.path === '/edinfo/changepassword' || to.path === '/edinfo/perjournal' || to.path === '/edinfo/acstar'
	|| to.path === '/edinfo/blogstar' || to.path === '/edinfo/blognews' || to.path === '/edinfo/blogpub' 
	|| to.path === '/edinfo/bloghistory'

	|| to.path === '/seinfo/changepassword' || to.path === '/seinfo/handleideas' || to.path === '/seinfo/acstar'
	|| to.path === '/seinfo/blogstar' || to.path === '/seinfo/blognews' || to.path === '/seinfo/blogpub' 
	|| to.path === '/seinfo/bloghistory' 

	|| to.path === '/userinfo/changepassword' || to.path === '/userinfo/acstar' || to.path === '/userinfo/blogstar'
	|| to.path === '/userinfo/blognews' || to.path === '/userinfo/blogpub' || to.path === '/userinfo/bloghistory'
	){
		window.alert("您还没有登录")
		return next({ path: '/login' });
	}	
	else {
		return next();
	}
  } else {
	if( status != 2) { //越级的情况
		if (to.path === '/adinfo') {
			window.alert("请登录管理员账号")
			return next({ path: '/login' });
		}
	}
	if( status != 1) { //越级的情况
		if (to.path === '/edinfo') {
			window.alert("请登录编辑账号")
			return next({ path: '/login' });
		}
	}
	if( status != 3) { //越级的情况
		if (to.path === '/seinfo') {
			window.alert("请登录客服账号")
			return next({ path: '/login' });
		}
	}
	if( status != 0) { //越级的情况
		if (to.path === '/userinfo') {
			window.alert("请登录用户账号")
			return next({ path: '/login' });
		}
	}
    return next();
  }
});
export default router
